﻿if not anon_cc_loaded then
	 PQR_LoadLua("PQR_CC_List.lua")
end

if not anon_general_functions_loaded then
	PQR_LoadLua("PQR_General_Functions.lua")
end

if not anon_damage_log_loaded then
	PQR_LoadLua("PQR_Anon_Damage_Log.lua")
end

if not cTar then
	cTar = {"target","focus","mouseover","arena1","arena2","arena3","arena4","arena5","arenapet1","arenapet2","arenapet3","arenapet4","arenapet5","pettarget"}
end

-- { checkEvery, lastCheck }
if timers == nil then timers = {} end

function SetTimer(timerName, checkEvery)
	if not timers[timerName] then
		timers[timerName] = { checkEvery = checkEvery, lastCheck = GetTime() }
	end	
end

function CheckTimer(timerName)
	if not timers[timerName] then
		return false
	end

	return GetTime() - timers[timerName].lastCheck >= timers[timerName].checkEvery
end

-- Just updates the last lastCheck field with current time
function UpdateTimer(timerName)
	if timers[timerName] then
		timers[timerName].lastCheck = GetTime()
	end
end

function InMelee()
	if UnitAffectingCombat("player") ~= nil
	and IsSpellInRange(GetSpellInfo(78),"target") == 1
	and UnitExists("target") ~= nil
	and UnitCanAttack("player","target") ~= nil
	then return true
	end
end

function CastClick()
  if IsMouseButtonDown(1) and MainMenuBar:IsShown() then 
    local mousefocus = GetMouseFocus() 
    if mousefocus and mousefocus.feedback_action 
    then SpellCancelQueuedSpell() PQR_DelayRotation(1) end
  end
end

stamBuff = { 6307, 103127, 90364, 21562} -- stam buff checks

function hasSTABuff()
	for i=1,#stamBuff do
		if UnitBuffID("player",stamBuff[i]) ~= nil then
			return true
		end
	end
	
	return false
end

immuneBuffList = {
	-- Druid
	[110617] = true, 	-- Deterrence (Symbiosis)
	[110696] = true,	-- Ice Block (Symbiosis)
	[110700] = true,	-- Divine Shield (Symbiosis)
	-- Hunter
	[19263] = true,		-- Deterrence
	-- Mage
	[45438] = true,		-- Ice Block
	-- Monk
	[122464] = true,	-- Dematerialize
	[122470] = true,	-- Touch of Karma
	-- Paladin
	[642] = true,		-- Divine Shield
}

magicImmuneBuffList = {
	-- Death Knight
	[48707] = true,		-- Anti-Magic Shell
	-- Druid
	[110570] = true,	-- Anti-Magic Shell (Symbiosis)
	[110617] = true, 	-- Deterrence (Symbiosis)
	[110696] = true,	-- Ice Block (Symbiosis)
	[110700] = true,	-- Divine Shield (Symbiosis)
	[110788] = true,	-- Cloak of Shadows (Symbiosis)
	[113002] = true,	-- Spell Reflection (Symbiosis)
	-- Hunter
	[19263] = true,		-- Deterrence
	-- Mage
	[45438] = true,		-- Ice Block
	-- Monk
	[122464] = true,	-- Dematerialize
	[122470] = true,	-- Touch of Karma
	-- Paladin
	[642] = true,		-- Divine Shield
	-- Priest
	-- Rogue
	[31224] = true,		-- Cloak of Shadows
	-- Shaman
	-- Warlock
	-- Warrior
	[23920] = true,	-- Spell Reflection
	[114028] = true	-- Mass Spell Reflect
}

-- same for general damage immunity
magicImmuneDebuffList = {
	-- Druid
	[33786] = true,		-- Cyclone
	-- Priest
	[113506] = true		-- Cyclone (Symbiosis)
}

slowImmuneBuffList = {
	[1044] = true,		-- Hand of Freedom
	[47585] = true,		-- Dispersion
	[46924] = true, 	-- Bladestorm
	[34692] = true, 	-- Beast Within
	[108273] = true, 	-- WindWalk
	[53271] = true 		-- Master's Call
}

reflectCC 		= { 
					-- DEATH KNIGHT
					-- DRUID
						33786,		-- Cyclone
					-- HUNTER
					-- MAGE
						28272,		-- Pig
						118,		-- Sheep
						61305,		-- Cat
						61721,		-- Rabbit
						61780,		-- Turkey
						28271,		-- Turtle
						113092,		-- Frost Bomb
					-- MONK
					-- PALADIN
						20066,		-- Repentance
						110301, 	-- Turn Evil
						145067,	 	-- Turn Evil
						10326,		-- Turn Evil
					-- PRIEST
						605,		-- Mind Control
					-- ROGUE
					-- SHAMAN
						51514,		-- Hex
					-- WARLOCK
						5782,		-- Fear
						118699		-- Fear
					-- WARRIOR
}
reflectDMG		= {
						-- DEATH KNIGHT
						-- DRUID
							78674,		-- Starsurge
						-- HUNTER
						-- MAGE
							113092,		-- Frost Bomb
							30451,		-- Arcane Blast
						-- MONK
						-- PALADIN
						-- PRIEST
							8092,		-- Mind Blast
						-- ROGUE
						-- SHAMAN
							51505,		-- Lava Burst
							117014,		-- Elemental Blast
						-- WARLOCK
							116858,		-- Chaos Bolt
							6353		-- Soul Fire
						-- WARRIOR
						}

-- Channeled ones
InterruptChannelSpells = {
	-- Death Knight
	-- Druid
	-- Hunter
	-- Mage
	-- Monk
	-- Paladin
	-- Priest
	-- Rogue
	-- Shaman
	-- Warlock
	1120		-- Drain Soul
	-- Warrior
}

-- Casted ones
InterruptSpells = {
	-- Death Knight
	
	-- Druid
	2637,		-- Hibernate
	33786,		-- Cyclone
	5185,		-- Healing Touch
	8936,		-- Regrowth
	50464,		-- Nourish
	339, 		-- Entangling Roots

	-- Hunter
	1513, 		-- Scare Beast
	982, 		-- Revive Pet

	-- Mage
	118,		-- Polymorph
	61305,		-- Polymorph (Black cat)
	28272,		-- Polymorph (Pig)
	61721,		-- Polymorph (Rabbit)
	61780,		-- Polymorph (Turkey)
	28271,		-- Polymorph (Turtle)
	82691,		-- Ring of Frost
	116,		-- Frostbolt
	30451, 		-- Arcane Blast
	113092,		 --Frost Bomb
	11366, 		--Pyroblast
	102051,		 --Frost Jaw
	126201,		 -- Frost Bolt
	44614, 		-- Frostfire Bolt
	133, 		-- Fireball

	-- Monk
	116694, 	-- Surging Mist
	124682, 	-- Enveloping Mist
	115151, 	-- Renewing Mist

	-- Paladin
	19750,		-- Flash of Light
	82326,		-- Divine Light
	20066, 		-- Repentance
	110301, 	-- Turn Evil
	145067,	 	-- Turn Evil
	10326,		-- Turn Evil
	635, 		-- Holy Light
	2812, 		-- Denounce
	82327, 		-- Holy Radiance

	-- Priest
	2061,		-- Flash Heal
	9484,		-- Shackle Undead
	605,		-- Dominate Mind
	129197,		-- Mind Flay (Insanity)
	8092, 		--Mind Blast
	2060,		-- Greater Heal
	124465,		-- Vampiric Touch
	--32375, 	-- Mass Dispel		(cast)
	596, 		-- Prayer of Healing

	-- Rogue
	
	-- Shaman
	331,		-- Healing Wave
	8004,		-- Healing Surge
	51505,		-- Lava Burst
	403,		-- Lightning Bolt
	77472,		-- Greater Healing Wave
	51514,		-- Hex
	1064,		-- Chain Heal
	73920, 		-- Healing Rain
	
	-- Warlock
	5782,		-- Fear
	48181,		-- Haunt
	30108,		-- Unstable Affliction
	116858, 	--Chaos Bolt
	111771, 	-- Demonic Gateway
	118297 		-- Immolate
	
	-- Warrior	

}
						-- Warrior's Shattering Throw buffs
shatterList = {
	[45438] = true,		-- Ice Block
	[110700] = true,	-- Divine Shield (Paladin)
	[110696] = true,	-- Ice Block (Mage)
	[45438] = true,		-- Ice Block
	[1022] = true, 		--Hand of Protection
	[642] = true		-- Divine Shield
}

disarmList = {
	-- DEATH KNIGHT (Pillar of Frost)
	[51271] = true,
	-- DRUID
	-- HUNTER (Bestial Wrath)
	[19574] = true,
	-- MAGE
	-- MONK
	-- PALADIN (Avenging Wrath)
	[31884] = true,
	-- PRIEST
	-- ROGUE (Shadow Dance / Adrenaline Rush)
	[51713] = true,
	[13750] = true,
	-- SHAMAN
	-- WARLOCK
	-- WARRIOR (Recklessness / Avatar)
	[1719] = true,
	[107574] = true
}

-- Warlock
function UnitNeedsDots(unitId)
	if unitId then
		if not UnitIsImmuneToMagic(unitId) then
			TargetHealth = UnitHealth(unitId) / UnitHealthMax(unitId) * 100
			
			--if debugInfo then print('Checking for:'..UnitName(unitId)) end
			
			if UnitHealthMax(unitId) > SmallAddsMaxHealth then
				local Agony, _, _, _, _, _, AgonyEndTime = UnitDebuffID(unitId, 980, "player")
				local Corruption, _, _, _, _, _, CorruptionEndTime = UnitDebuffID(unitId, 172, "player")
				local UnstableAffliction, _, _, _, _, _, UnstableAfflictionEndTime = UnitDebuffID(unitId, 30108, "player")
				local Haunt, _, _, _, _, _, HauntEndTime = UnitDebuffID(unitId, 48181, "player")
	
				if not Agony or AgonyEndTime - GetTime() <= 11.5 then
					if SpellCheck(980, unitId) then
						return true
					end
				end
	
				if not Corruption or CorruptionEndTime - GetTime() <= 9.0 then
					if SpellCheck(172, unitId) then
						return true
					end
				end

				if not UnstableAffliction or UnstableAfflictionEndTime - GetTime() <= 7.5 then
					if SpellCheck(30108, unitId) and not PQR_IsMoving(0.3) then
						return true
					end
				end
	
				--if CheckTimer("lastHaunt") then
				if GetTime() - lastHauntCast > 3 then
					if (not Haunt or HauntEndTime <= 2.7) and Soulshards > 1 and PlayerFacingTarget(unitId, 0) then
						if SpellCheck(48181, unitId) and not PQR_IsMoving(0.3) then
							return true
						end
					end
				end
			end
		end
	end
	
	return false
end

function GetSpellCD(spellID)
	local start, duration, enabled = GetSpellCooldown(spellID)
	
	if start then
		return (start + duration - GetTime())
	end
	
	return -1
end

-- Only used by canSpellReflect()
function checkSpellReflect(unitId, castStartTime, castEndTime)
	local timeSinceStart = (GetTime() * 1000 - castStartTime) / 1000 + (tonumber((select(3,GetNetStats()) + select(4,GetNetStats())) / 2000)) 
	local timeLeft = ((GetTime() * 1000 - castEndTime) * -1) / 1000
	local castTime = castEndTime - castStartTime
	local currentPercent = timeSinceStart / castTime * 100000

	if	timeLeft < 26
		and UnitIsUnit(unitId.."target", "player")
	then
		return true
	end
	
	return false
end

function canSpellReflect(unitId)
	if unitId then	
		local castName, _, _, _, castStartTime, castEndTime, _, _, castInterruptable = UnitCastingInfo(unitId)
		
		if not castName then return spellId end
		
		for i = 1, #reflectCC do
			if castName == GetSpellInfo(reflectCC[i]) then
				if checkSpellReflect(unitId, castStartTime, castEndTime) then return true end
			end
		end
		
		for i = 1, #reflectDMG do
			if castName == GetSpellInfo(reflectDMG[i]) then
				if checkSpellReflect(unitId, castStartTime, castEndTime) then return true end
			end
		end		
	end
	
	return false
end

-- Warrior interrupt (add new ones later)
-- 6552 pummel
-- 102060 dis shout
function canInterrupt(unitId)
	local castName, _, _, _, castStartTime, castEndTime, _, _, castInterruptable = UnitCastingInfo(unitId)

	for _, v in ipairs(InterruptSpells) do
		if GetSpellInfo(v) == castName then
				local timeSinceStart = (GetTime() * 1000 - castStartTime) / 1000 + (tonumber((select(3,GetNetStats()) + select(4,GetNetStats())) / 2000)) 
				local timeLeft = ((GetTime() * 1000 - castEndTime) * -1) / 1000
				local castTime = castEndTime - castStartTime
				local currentPercent = timeSinceStart / castTime * 100000

			if currentPercent >= 58
			then
				local class = select(2, UnitClass("player"))
	
				if class == "WARRIOR" then
					if	select(2,GetSpellCooldown(6552)) == 0
						and InRange(78, unitId)
					then
						return 6552 -- Pummel
					end
					
					if	IsSpellKnown(102060)
						and select(2,GetSpellCooldown(102060)) == 0
						and CheckInteractDistance(unitId,3)
					then
						return 102060 -- Disrupting Shout
					end
				elseif class == "PALADIN" then
					if	select(2,GetSpellCooldown(96231)) == 0
						and InRange(96231, unitId)
					then
						return 96231 -- Rebuke
					end
					
					if	IsSpellKnown(28730)
						and select(2,GetSpellCooldown(28730)) == 0
						and CheckInteractDistance(unitId,3)
					then
						return 28730 -- Arcane Torrent
					end
				end
			end
		end
	end
	
	return 0
end
